{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "fad1536b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.b45f989a570d91d829147764f836158d.315360000.1964957964.282335-25904771', 'expires_in': 2592000, 'session_key': '9mzdDx2cyNr9TgyFDiwZUaYFvAtQj9fMxeD+m+wGrsl3vK5v8R5kzVqua2AngTcXOIy9uxHTAfdrrzsRjbKCeeA6Pkg2pg==', 'access_token': '24.49c9fa2c62235d67e6ee237faa3c5239.2592000.1652189964.282335-25904771', 'scope': 'public vis-classify_dishes vis-classify_car brain_all_scope vis-classify_animal vis-classify_plant brain_object_detect brain_realtime_logo brain_dish_detect brain_car_detect brain_animal_classify brain_plant_classify brain_ingredient brain_advanced_general_classify brain_custom_dish brain_poi_recognize brain_vehicle_detect brain_redwine brain_currency brain_vehicle_damage brain_multi_ object_detect wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online', 'session_secret': 'e1ce3de461358abd85979796071576e7'}\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'24.49c9fa2c62235d67e6ee237faa3c5239.2592000.1652189964.282335-25904771'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 为搭建一个Flask框架下，集食物图片识别及吃什么推荐的应用，需上传图片功能、抽奖功能、界面设计、结果处理等\n",
    "# 现于jupyter实践基础AI功能\n",
    "# 设计者：蔡洪嘉\n",
    "\n",
    "# 首先定义access_token\n",
    "# encoding:utf-8\n",
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=eREailrg19DA07HXwIQN4d60&client_secret=rxkzNhc0wCYftv6KaMOgafwrktTyVmEm'\n",
    "response = requests.get(host)\n",
    "if response:\n",
    "    print(response.json())\n",
    "access_token=response.json()['access_token']\n",
    "access_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "7188b6a9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result_num': 5, 'result': [{'calorie': '141', 'has_calorie': True, 'name': '寿司', 'probability': '0.967076'}, {'calorie': '143', 'has_calorie': True, 'name': '紫菜包饭', 'probability': '0.0111145'}, {'has_calorie': False, 'name': '反卷', 'probability': '0.00466019'}, {'calorie': '155', 'has_calorie': True, 'name': '三文鱼刺身', 'probability': '0.00393271'}, {'calorie': '184', 'has_calorie': True, 'name': '鳗鱼卷', 'probability': '0.00212592'}], 'log_id': 1513149652206807037}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "菜品识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v2/dish\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('food.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img,\"top_num\":5}\n",
    "access_token = access_token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "e8dfe12e",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-38-cfce790db944>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-38-cfce790db944>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    '识别一：'+response.json()['result'][0]['name']+'  置信度：'+str(response.json()['result'][0]['probability'])[:4]+ '%'+'识别二：'+response.json()['result'][1]['name']+'  置信度：'+str(response.json()['result'][1]['probability'])[:4]+ '%'+\u001b[0m\n\u001b[1;37m                                                                                                                                                                                                                                  ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "'识别一：'+response.json()['result'][0]['name']+'  置信度：'+str(response.json()['result'][0]['probability'])[:4]+ '%'\n",
    "'识别二：'+response.json()['result'][1]['name']+'  置信度：'+str(response.json()['result'][1]['probability'])[:4]+ '%'\n",
    "'识别三：'+response.json()['result'][2]['name']+'  置信度：'+str(response.json()['result'][2]['probability'])[:4]+ '%'\n",
    "'识别四：'+response.json()['result'][3]['name']+'  置信度：'+str(response.json()['result'][3]['probability'])[:4]+ '%'\n",
    "'识别五：'+response.json()['result'][4]['name']+'  置信度：'+str(response.json()['result'][4]['probability'])[:4]+ '%'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "d424a528",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'0.96%'"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str(response.json()['result'][0]['probability'])[:4]+ '%'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "d0e556f6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "识别一：寿司  置信度：0.96%\n",
      "识别二：紫菜包饭  置信度：0.01%\n",
      "识别三：反卷  置信度：0.00%\n",
      "识别四：三文鱼刺身  置信度：0.00%\n",
      "识别五：鳗鱼卷  置信度：0.00%\n"
     ]
    }
   ],
   "source": [
    "print('识别一：'+response.json()['result'][0]['name']+'  置信度：'+str(response.json()['result'][0]['probability'])[:4]+ '%'+'\\n'\n",
    "'识别二：'+response.json()['result'][1]['name']+'  置信度：'+str(response.json()['result'][1]['probability'])[:4]+ '%'+'\\n'\n",
    "'识别三：'+response.json()['result'][2]['name']+'  置信度：'+str(response.json()['result'][2]['probability'])[:4]+ '%'+'\\n'\n",
    "'识别四：'+response.json()['result'][3]['name']+'  置信度：'+str(response.json()['result'][3]['probability'])[:4]+ '%'+'\\n'\n",
    "'识别五：'+response.json()['result'][4]['name']+'  置信度：'+str(response.json()['result'][4]['probability'])[:4]+ '%')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "09f876d2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "huan \n",
      " xing \n"
     ]
    }
   ],
   "source": [
    "print('huan \\n xing ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "285d5454",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'huan \\n xing '"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'huan \\n xing '"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "293b5db6",
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'easyxlsx'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-58-ef987ef66b70>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0measyxlsx\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mSimpleWriter\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'easyxlsx'"
     ]
    }
   ],
   "source": [
    "from easyxlsx import SimpleWriter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "831dee8e",
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'easyxlsx'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-59-ef987ef66b70>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0measyxlsx\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mSimpleWriter\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'easyxlsx'"
     ]
    }
   ],
   "source": [
    "from easyxlsx import SimpleWriter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "441de2cd",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
